package com.lottery.ruok.lottrey.ext;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.DrawableRes;
import android.view.View;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.lottery.ruok.lottrey.R;

public class GlideExt {
    /**
     * 使用网络图片给View设置背景
     *
     * @param context
     * @param imageUrl
     * @param view
     * @param defImageRes
     */
    public static void setBackgroundDrawable(Context context, String imageUrl, final View view,@DrawableRes Integer defImageRes) {
        DrawableTypeRequest<String> build = Glide.with(context).load(imageUrl);
        if (defImageRes != null) {
            build.placeholder(defImageRes)
                    .error(defImageRes);
        }

        build.into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                view.setBackgroundDrawable(resource.getCurrent());
            }
        });
    }

}
